/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utain.quiz1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author Nut
 */
public class UtainXMLFileWriter extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            createRssDoc();
            PrintWriter pw = response.getWriter();
            pw.print("<a href='http://localhost:8080/Utain-quiz1-2013/books2.xml'>The XML file</a> was file was create successfully");
        } catch (Exception ex) {
            Logger.getLogger(UtainXMLFileWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void createRssDoc() throws Exception {
        String filename = "C:\\Users\\Nut\\Documents\\utain-lab4-parser2\\XML-lab4-1\\Utain-quiz1-2013\\web\\books2.xml";
        File file = new File(filename);
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xtw = xof.createXMLStreamWriter(new OutputStreamWriter(new FileOutputStream(file)));
        xtw.writeStartDocument("UTF-8", "1.0");
        {
            xtw.writeStartElement("catalog");
            {
                xtw.writeStartElement("book");
                xtw.writeAttribute("id", "bk101");
                {
                    xtw.writeStartElement("title");
                    xtw.writeCharacters("ประชุมโครงโลกนิติ");
                    xtw.writeEndElement();

                    xtw.writeStartElement("price");
                    xtw.writeCharacters("40");
                    xtw.writeEndElement();

                    xtw.writeStartElement("publish_year");
                    xtw.writeAttribute("type", "Buddhist calendar");
                    xtw.writeCharacters("2384");
                    xtw.writeEndElement();

                }
                xtw.writeEndElement();

                xtw.writeStartElement("book");
                xtw.writeAttribute("id", "bk101");
                {
                    xtw.writeStartElement("title");
                    xtw.writeCharacters("What I Wish I Knew When I Was 20");
                    xtw.writeEndElement();

                    xtw.writeStartElement("price");
                    xtw.writeCharacters("18.32");
                    xtw.writeEndElement();

                    xtw.writeStartElement("publish_year");
                    xtw.writeAttribute("type", "Georgian calendar");
                    xtw.writeCharacters("2009");
                    xtw.writeEndElement();

                    xtw.writeStartElement("description");
                    xtw.writeCharacters("Opportunities are abundunt, but you need to identify problems");
                    {
                        xtw.writeStartElement("thai");
                        xtw.writeCharacters("นวัตกรรมมากมายสามารถออกมาจากตัวเรา ได้หากเราไม่ยึดติดกับความรู้เดิมๆ พื้นที่ปลอดภัย");
                        xtw.writeEndElement();
                    }
                    xtw.writeEndElement();

                }
                xtw.writeEndElement();
            }
            xtw.writeEndElement();//end qoutes
        }
        xtw.writeEndDocument();//end doc
        xtw.flush();
        xtw.close();
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
